package org.datastructure.firstday.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Graph {

    private List<String> vertexList;
    private int numOfEdges;
    private int[][] edges;
    private boolean[] isVisited;

    public static void main(String[] args) {
        String Vertexs[] = {"A", "B", "C", "D", "E"};

        //测试一把图是否创建ok
        int n = Vertexs.length;  //结点的个数
        //创建图对象
        Graph graph = new Graph(n);
        //循环的添加顶点
        for (String vertex : Vertexs) {
            graph.insertVertex(vertex);
        }
        //添加边
        //A-B A-C B-C B-D B-E
        graph.insertEdge(0, 1, 1); // A-B
        graph.insertEdge(0, 2, 1); //
        graph.insertEdge(1, 2, 1); //
        graph.insertEdge(1, 3, 1); //
        graph.insertEdge(1, 4, 1); //
        //显示一把邻结矩阵
        graph.showGraph();

        //测试一把，我们的dfs遍历是否ok
        System.out.println("深度遍历");
        graph.dfs(); // A->B->C->D->E [1->2->4->8->5->3->6->7]

        System.out.println("广度遍历");
        graph.bfs();
    }

    public Graph(int num) {
        vertexList = new ArrayList<>(num);
        numOfEdges = 0;
        edges = new int[num][num];

    }


    //对dfs 进行一个重载, 遍历我们所有的结点，并进行 dfs
    public void dfs() {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < vertexList.size(); i++) {
            if (!isVisited[i]) {
                dfs(isVisited, i);
            }
        }
    }


    //深度优先遍历算法
    //i 第一次就是 0
    public void dfs(boolean[] isVisited, int index) {
        System.out.println(vertexList.get(index));
        isVisited[index] = true;

        int w = getFirstNeighbor(index);
        while (w != -1) {
            if (!isVisited[w]) {
                dfs(isVisited, w);
            }
            w = getNextNeighbor(index, w);
        }
    }

    /**
     *广度优先遍历
     */
    public void bfs() {
        isVisited = new boolean[vertexList.size()];
        for (int i = 0; i < vertexList.size(); i++) {
            if (!isVisited[i]) {
                bfs(isVisited, i);
            }
        }
    }

    /**
     * 广度优先遍历
     * @param isVisited
     * @param i
     */
    public void bfs(boolean[] isVisited, int i) {

        int u, w;
        LinkedList<Integer> linked = new LinkedList<>();

        System.out.println(vertexList.get(i));
        isVisited[i] = true;
        u = getFirstNeighbor(i);
        linked.addLast(u);
        while (!linked.isEmpty()) {
            w = linked.removeFirst();
            while (w != -1) {
                if (!isVisited[w]) {
                    System.out.println(vertexList.get(w) + "=>");
                    linked.addLast(w);
                    isVisited[w] = true;
                }
                w = getNextNeighbor(u, w);
            }
        }
    }


    //得到第一个邻接结点的下标 w

    /**
     * @param index
     * @return 如果存在就返回对应的下标，否则返回-1
     */
    public int getFirstNeighbor(int index) {
        for (int j = 0; j < vertexList.size(); j++) {
            if (edges[index][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    //根据前一个邻接结点的下标来获取下一个邻接结点
    public int getNextNeighbor(int v1, int v2) {
        for (int j = v2 + 1; j < vertexList.size(); j++) {
            if (edges[v1][j] > 0) {
                return j;
            }
        }
        return -1;
    }

    //图中常用的方法
    //返回结点的个数

    /**
     * 返回一共又多少个顶点
     *
     * @return
     */
    public int getNumOfVertex() {
        return vertexList.size();
    }

    //显示图对应的矩阵
    public void showGraph() {
        for (int[] link : edges) {
            System.err.println(Arrays.toString(link));
        }
    }

    //得到边的数目
    public int getNumOfEdges() {
        return numOfEdges;
    }

    //返回结点i(下标)对应的数据 0->"A" 1->"B" 2->"C"
    public String getValueByIndex(int i) {
        return vertexList.get(i);
    }

    //返回v1和v2的权值
    public int getWeight(int v1, int v2) {
        return edges[v1][v2];
    }

    //插入结点
    public void insertVertex(String vertex) {
        vertexList.add(vertex);
    }
    //添加边

    /**
     * @param v1     表示点的下标即使第几个顶点  "A"-"B" "A"->0 "B"->1
     * @param v2     第二个顶点对应的下标
     * @param weight 表示
     */
    public void insertEdge(int v1, int v2, int weight) {
        edges[v1][v2] = weight;
        edges[v2][v1] = weight;
        numOfEdges++;
    }
}
